package com.itheima.d1_reflect_constructor;

import org.junit.jupiter.api.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 获取一个构造器并使用
 */
public class demo2 {
    @Test
    public void getDeclareConstructor() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //1.获取类对象
        Class c = Student.class;

        //2.无参、有参
        Constructor constructor = c.getDeclaredConstructor();
        Constructor constructor1 = c.getConstructor(String.class,int.class,String.class);

        //3.私有构造器暴力反射，获取权限
        constructor.setAccessible(true);

        //4.使用该类创建对象
        Student s = (Student) constructor.newInstance();
        Student s1 = (Student) constructor1.newInstance("老鼠人",20,"男");
        System.out.println(s);
        System.out.println(s1);
    }
}
